<?php
namespace app\faxing\controller;

use app\common\model\Gameulog;
use app\common\model\Player;
use think\cache\driver\Redis;
use think\Controller;
use think\Db;
use app\common\controller\FaXingBase;
use think\facade\Log;
use think\facade\Session;
use think\Loader;
use think\facade\App;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Game extends FaXingBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $game = $this->game;

        //未登录 登录地址为空
        if(!$this->token){
            $game['api_login_url'] = "";
        }

        $this->assign('game', $game);
        return $this->fetch();
    }

    /**
     * 注册
     */
    public function register(){
        $params = $this->request->request();

        // 检验签名 初始化对控制器，调用签名验证方法，默认调用公共方法
        $channelName = ucfirst($this->channel['username']);
        // 动态加载自定义类（例如：app\common\CustomClass
        $className ='app\faxing\channel\\'.$channelName; // 替换为你的类名
        if (!class_exists($className)) {
            Log::info("渠道类初始化失败");
        }
        //订单校验
        $gameapi = new $className();
        $result = $gameapi->register($params);
        $this->success("登录成功","",$result);
    }

    /**
     * 获取游戏链接
     */
    public function getGameUrl(){
        $params = $this->request->request();
        $game_url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST']."/aiqiH5/play/".$this->gid.'/'.$this->cid."?".http_build_query($params);
        echo json_encode(['ReturnCode'=>0,'ReturnMessage'=>'成功','ReturnData'=>$game_url]);exit;
    }

    /**
     * 获取角色信息
     */
    public function getRole()
    {
        $params = $this->request->request();
        // 检验签名 初始化对控制器，调用签名验证方法，默认调用公共方法
        $channelName = ucfirst($this->channel['username']);
        // 动态加载自定义类（例如：app\common\CustomClass
        $className ='app\faxing\channel\\'.$channelName; // 替换为你的类名
        if (!class_exists($className)) {
            Log::info("渠道类初始化失败");
        }
        //订单校验
        $gameapi = new $className();
        $gameapi->getRole($params);

    }

    /**
     * 后端创建角色
     */
    public function create_role()
    {
        $params = $this->request->post();

        // 检验签名 初始化对控制器，调用签名验证方法，默认调用公共方法
        $channelName = ucfirst($this->channel['username']);
        // 动态加载自定义类（例如：app\common\CustomClass
        $className ='app\faxing\channel\\'.$channelName; // 替换为你的类名
        if (!class_exists($className)) {
            Log::info("渠道类初始化失败");
        }
        //订单校验
        $gameapi = new $className();
        $result = $gameapi->create_role($params);
        $this->success("创建成功","",$result);
    }

    private function check_sign($params,$key)
    {
        unset($params[1]);
        $sign = $params['sign'];
        unset($params['sign']);
        unset($params['rp_token']);
        $check_sign = get_sign($params,$key);
        if($check_sign != $sign){
            return false;
        }
        return true;
    }

}